package com.lkd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.PartnerDTO;
import com.lkd.entity.Partner;
import com.lkd.mapper.PartnerMapper;
import com.lkd.result.TableInfo;
import com.lkd.service.PartnerService;
import com.lkd.vo.PartnerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/17 11:10
 * @Description:
 **/
@Service
public class PartnerServiceImpl extends ServiceImpl<PartnerMapper, Partner> implements PartnerService {
    private final PartnerMapper partnerMapper;

    @Autowired
    public PartnerServiceImpl(PartnerMapper partnerMapper) {
        this.partnerMapper = partnerMapper;
    }

    /**
     * @Description 新增
     * @Param [partnerDto]
     * @Date 2025/8/17 11:12
     **/
    public void save(PartnerDTO partnerDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDto, partner);
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        partnerMapper.insert(partner);
    }

    @Override
    public TableInfo<Partner> list(PartnerDTO partnerDto) {
        PageHelper.startPage(partnerDto.getPageIndex(), partnerDto.getPageSize());
        List<Partner> partnerList = partnerMapper.queryPartnerByName(partnerDto.getName());
        Page<Partner> page = (Page<Partner>) partnerList;
        return new TableInfo<>(partnerDto.getPageIndex()
                , partnerDto.getPageSize()
                , page.getPageNum()
                , page.getTotal()
                , partnerList);
    }

    @Override
    public PartnerVO partnerId(String partnerId) {
        Partner partner = partnerMapper.getPartnerById(partnerId);
        PartnerVO partnerVO = new PartnerVO();
        BeanUtils.copyProperties(partner, partnerVO);
        return partnerVO;
    }

    @Override
    public boolean update(PartnerDTO partnerDto) {
        Partner partner = new Partner();
        BeanUtils.copyProperties(partnerDto, partner);
        partner.setUpdateTime(LocalDateTime.now());
        return partnerMapper.update(partner);
    }

    @Override
    public boolean delete(String id) {
        return partnerMapper.delete(id);
    }

    @Override
    public boolean resetPwd(String id) {
        Partner partner = partnerMapper.getPartnerById(id);
        partner.setPassword("123456");
        return partnerMapper.update(partner);
    }
}
